package com.qms.repast.ui.base;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.orhanobut.logger.Logger;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import butterknife.ButterKnife;
import cn.jiguang.analytics.android.api.BrowseEvent;
import cn.jiguang.analytics.android.api.JAnalyticsInterface;

/**
 * @author 泅渡者
 * @date 2018/6/6 0006.
 * @describe: 基础Activity进行统一封装处理
 */

public abstract class BaseActivity<P extends BasePresenter> extends RxAppCompatActivity {

	/**
	 * 是否沉浸式
	 */
	protected boolean isTrans;
	private ConnectivityManager manager;
	protected P mPresenter;
	protected long timerIn;
	protected long timerOut;

	@Override
	protected void onCreate (@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
			finish();
			return;
		}
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			initStatusBar(isTrans);
		}
		setContentView(initLayout());
		timerIn = System.currentTimeMillis();
		//初始化ButterKnife
		ButterKnife.bind(this);
		initPresenter(getIntent());
		checkPresenterIsNull();
		initView();
		JAnalyticsInterface.onPageStart(this, this.getClass().getCanonicalName());
		initData();

	}

	@Override
	public void onWindowFocusChanged (boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
	}

	/**
	 * 加载布局
	 *
	 * @return
	 */
	protected abstract int initLayout ();

	/**
	 * 注入Presenter
	 *
	 * @param intent
	 */
	protected abstract void initPresenter (Intent intent);

	/**
	 * 初始化布局
	 */
	protected abstract void initView ();

	/**
	 * 加载部分数据
	 */
	protected abstract void initData ();

	private void checkPresenterIsNull () {
		if (mPresenter == null) {
			Logger.e("请实例化Presenter!");
			throw new IllegalStateException("please init mPresenter in initPresenter() method !");
		}
	}

	@Override
	protected void onResume () {
		super.onResume();
	}

	@Override
	protected void onPause () {
		super.onPause();
	}

	@Override
	protected void onStop () {
		super.onStop();
	}

	@Override
	public void onBackPressed () {
		super.onBackPressed();
	}

	@Override
	protected void onDestroy () {
		super.onDestroy();
		mPresenter.detachView();
		timerOut = System.currentTimeMillis();
		new BrowseEvent(this.getClass().getCanonicalName(), this.getClass().getCanonicalName(), "Activity", (timerOut - timerIn));
		JAnalyticsInterface.onPageEnd(this, this.getClass().getCanonicalName());
	}

	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	public void initStatusBar (boolean isTransparent) {
		Window window = getWindow();
		window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		if (isTransparent) {
			window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
																| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
		} else {
			window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
																| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
		}
		isTrans = isTransparent;
		window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
		window.setStatusBarColor(Color.WHITE);
	}

	/**
	 * 检查网络
	 *
	 * @return 是否有网络
	 */
	public boolean checkNetworkState () {
		boolean flag = false;
		manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (manager.getActiveNetworkInfo() != null) {
			flag = manager.getActiveNetworkInfo().isAvailable();
		}
		return flag;
	}

}
